package com.dataseek.iot.mqtt.receiver;

import com.dataseek.iot.mqtt.service.MqttReceiveService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.mqtt.support.MqttHeaders;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHandler;
import org.springframework.messaging.MessagingException;
import org.springframework.stereotype.Component;


/**
 * mqtt订阅消息处理
 * 与callback 相斥
 */
@Component
@Slf4j
public class MqttMessageReceiver implements MessageHandler {


    @Autowired
    MqttReceiveService receiveService;



    @Override
    public void handleMessage(Message<?> message) throws MessagingException {
        String topic = String.valueOf(message.getHeaders().get(MqttHeaders.RECEIVED_TOPIC));
        String payload = String.valueOf(message.getPayload());
        receiveService.handlerMqttMessage(topic, payload);
    }

}
